package com.shanghaionstar.user.service;

import android.content.Context;
import android.widget.EditText;

import com.onstar.cn.api.UsersService;
import com.onstar.cn.common.exception.OnStarException;
import com.onstar.cn.common.util.Constants;

import com.onstar.cn.framework.annotation.http.HeadsValue;
import com.onstar.cn.framework.annotation.http.Request;
import com.onstar.cn.framework.mvp.base.core.BaseService;
import com.onstar.cn.framework.network.HttpInjectUtils;
import com.onstar.cn.framework.network.entity.RequestEntity;
import com.onstar.cn.framework.network.enumtype.HttpAccept;
import com.onstar.cn.framework.network.enumtype.HttpMethod;
import com.onstar.cn.framework.network.enumtype.Language;
import com.onstar.cn.framework.utils.GsonHelper;
import com.onstar.cn.more.entity.SubscriberDTO;
import com.shanghaionstar.utils.BusinessUtil;
import com.shanghaionstar.utils.NetConnectUtil;

import java.util.Locale;

import rx.Observable;
import rx.functions.Func0;

/**
 * Created by Mengzhou Gu on 2016/8/4.
 */
public class PayCardVisitorActiveService extends BaseService<SubscriberDTO> {
    private Context  context;
    private EditText et_car_identify;

    public PayCardVisitorActiveService(Context context, EditText et_car_identify) {
        this.context = context;
        this.et_car_identify = et_car_identify;
    }

    @Override
    public RequestEntity getRequestEntity() {
        return null;
    }

    @Override
    public SubscriberDTO jsonToBean(String json) {
        return GsonHelper.fromJson(json,SubscriberDTO.class);
    }


    @Request(heads = @HeadsValue(accept = HttpAccept.JSON, language = Language.AUTO), method = HttpMethod.GET)
    public Observable<SubscriberDTO> getObserval() {
        return Observable.defer(new Func0<Observable<SubscriberDTO>>() {
            @Override
            public Observable<SubscriberDTO> call() {
                try {
                    UsersService service = new UsersService(Constants.CURRENT_SERVER_URL);
                    RequestEntity entity = NetConnectUtil.getAuthRequestEntity(context);
                    entity = service.getAccountInfoByVin(entity, et_car_identify.getText().toString().toUpperCase(Locale.CHINA));
                    entity = HttpInjectUtils.injectHttp(PayCardVisitorActiveService.this.getClass(), "getObserval", entity);

                    return Observable.just( GsonHelper.fromJson( BusinessUtil.getResponse(getDatas(entity), context),SubscriberDTO.class));
                } catch (OnStarException e) {
                    return Observable.error(e);
                }
            }

        });

    }
}
